﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gtkj.CMS.DAL
{
    public partial class DBDataSession
    {
        //BLL层调用此属性来访问DAL层
        public DbContext DB
        {
            get { return Gtkj.CMS.DAL.DbContextFactory.CreateDbContext(); }
        }

        //将多个表的操作合并到一次save
        public bool SaveChange()
        {
            try
            {
                return DB.SaveChanges() > 0;
            }
            catch (DbEntityValidationException ex)
            {
               // Common.LogHelper.WriteLogFile("DALFactory.DBDataSession.SaveChange:" + ex.ToString());
                throw ex;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 执行delete,insert,update
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public int ExecuteSql(string sql, params System.Data.SqlClient.SqlParameter[] pars)
        {
            return DB.Database.ExecuteSqlCommand(sql, pars);
        }

        /// <summary>
        /// 执行select
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public List<T> ExecuteSelectQuery<T>(string sql, params System.Data.SqlClient.SqlParameter[] pars)
        {
            return DB.Database.SqlQuery<T>(sql, pars).ToList();
        }
    }
}
